# Login/models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
import uuid


class User(AbstractUser):
    # 用户角色选择
    USER_TYPE_CHOICES = [
        ('jobseeker', '求职者'),  # 我要找工作
        ('recruiter', '招聘者'),  # 我要招聘
    ]
    
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    phone = models.CharField(max_length=11, unique=True, verbose_name="手机号")
    user_type = models.CharField(max_length=20, choices=USER_TYPE_CHOICES, default='jobseeker',verbose_name="用户角色")
    is_realname_authenticated = models.BooleanField(default=False, verbose_name="是否实名认证")
    id_card_number = models.CharField(max_length=18, blank=True, null=True, verbose_name="身份证号")
    id_card_front = models.CharField(max_length=255, blank=True, null=True, verbose_name="身份证正面")
    id_card_back = models.CharField(max_length=255, blank=True, null=True, verbose_name="身份证背面")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email']

    class Meta:
        verbose_name = "用户"
        verbose_name_plural = "用户"

    def __str__(self):
        return self.username or self.phone


class Order(models.Model):
    """订单模型 - 用于微信支付"""
    # 订单状态
    ORDER_STATUS_CHOICES = [
        ('pending', '待支付'),
        ('paid', '已支付'),
        ('cancelled', '已取消'),
        ('refunded', '已退款'),
    ]

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, verbose_name="订单ID")
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='orders',verbose_name="用户")
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="订单金额（元）")
    paid = models.BooleanField(default=False, verbose_name="是否已支付")
    out_trade_no = models.CharField(max_length=64, blank=True, null=True, unique=True,verbose_name="商户订单号")
    transaction_id = models.CharField(max_length=64, blank=True, null=True,verbose_name="微信支付订单号")
    paid_time = models.DateTimeField(blank=True, null=True, verbose_name="支付时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    status = models.CharField(max_length=20,choices=ORDER_STATUS_CHOICES,default='pending',verbose_name="订单状态")
    description = models.CharField(max_length=255,blank=True,null=True,verbose_name="订单描述")

    class Meta:
        verbose_name = "订单"
        verbose_name_plural = "订单"
        ordering = ['-created_at']
        db_table = 'order'

    def __str__(self):
        username = getattr(self.user, 'username', None) or getattr(
            self.user, 'phone', 'Unknown'
        )
        return f"订单-{self.id} (用户: {username}, 金额: ¥{self.amount})"